<template>
  <div class="app-container">
    <!-- 表单 -->
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="任务名称" prop="taskName">
        <el-input
          v-model="queryParams.taskName"
          placeholder="请输入任务名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="任务状态" prop="taskStatus">
        <el-select v-model="queryParams.taskStatus" placeholder="请选择任务状态" clearable size="small">
          <el-option
            v-for="dict in taskStatusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="标注类型" prop="markType">
        <el-select v-model="queryParams.markType" placeholder="请选择标注类型" clearable size="small">
          <el-option
            v-for="dict in markTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-tooltip effect="light" content="创建" placement="right-end">
          <el-button circle icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['mark:qatask:add']" />
        </el-tooltip>
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-tooltip effect="light" content="上一级" placement="right-end">-->
      <!--          <el-button circle icon="el-icon-upload2" size="mini" @click="handleReturn"/>-->
      <!--        </el-tooltip>-->
      <!--      </el-col>-->
      <el-col :span="21">
        <el-tooltip effect="light" content="批量修改" placement="right-end">
          <el-button
            circle
            icon="el-icon-edit"
            size="mini"
            @click="handleBatch"
            v-show="checkRoleMethod(['admin'])"
            :disabled="multiple"
          />
        </el-tooltip>
      </el-col>
      <el-col :span="1.5">
        <el-tooltip effect="light" content="批量汇总" placement="right-end">
          <el-button
            circle
            icon="el-icon-arrow-up"
            size="mini"
            @click="handleBatchSummarize"
            v-if="checkRoleMethod(['admin'])"
            :disabled="multiple || !(queryParams.parentId !== 0 && taskPath != '' && taskPath.indexOf('@') === -1)"
          />
        </el-tooltip>
      </el-col>
      <el-col :span="1.5">
        <el-tooltip effect="light" content="标注结果导出" placement="right-end">
          <el-button
            circle
            size="mini"
            icon="el-icon-download"
            @click="handleResultExport()"
            v-if="checkRoleMethod(['admin'])"
            :disabled="queryParams.parentId === 0"
          />
        </el-tooltip>
      </el-col>
      <el-col v-if="taskPath !== null && taskPath !== ''">
        <el-page-header @back="handleReturn" style="font-size: 12px; color: gray">
          <template #content>
            <el-breadcrumb separator-class="el-icon-arrow-right">
              <el-breadcrumb-item
                v-for="(title, titleIndex) in taskPath.split('@').length"
                :key="titleIndex"
                style="margin-top: 5px; font-size: 12px; color: gray"
              >
                <a style="font-weight: bold"> {{ taskPath.split('@')[titleIndex] }}</a>
              </el-breadcrumb-item>
            </el-breadcrumb>
          </template>
        </el-page-header>
      </el-col>
    </el-row>

    <!-- 表格 -->
    <el-table v-loading="loading" :data="qaTaskList" @selection-change="handleSelectionChange">
      <!-- 勾选框 -->
      <el-table-column type="selection" width="55" align="center" :selectable="checkboxSelect" />

      <el-table-column label="名称">
        <template slot-scope="scope">
          <!-- 图标 -->
          <el-button
            circle
            :type="scope.row.taskName ? `warning` : scope.row.fileId ? `success` : `info`"
            size="medium"
            :icon="scope.row.taskName ? `el-icon-folder` : scope.row.fileId ? `el-icon-document` : `el-icon-document`"
            @click="enterTask(scope.row)"
          >
          </el-button>
          <!-- 文件名称 -->
          <el-button size="mini" type="text" @click="enterTask(scope.row)" v-show="scope.row.taskName">
            {{ scope.row.taskName }}
          </el-button>
          <el-button size="mini" type="text" @click="enterOnlineCheck(scope.row)" v-show="scope.row.fileId">
            {{ scope.row.fileId }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="状态" width="100" prop="taskStatus" :formatter="taskStatusFormat" />
      <el-table-column label="标注类型" width="100" prop="markType" :formatter="markTypeFormat" />
      <!-- 操作 -->
      <el-table-column label="操作" width="240" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!--https://blog.csdn.net/zxy010714/article/details/108594820 -->
          <!-- 自动质检 -->
          <el-button
            v-show="scope.row.taskStatus == 1 || scope.row.taskStatus == 3 || scope.row.taskStatus == 4"
            size="mini"
            type="text"
            icon="el-icon-search"
            @click="handleAutoCheck(scope.row)"
            v-if="checkRoleMethod(['admin', 'checker', 'marker'])"
          >
            自动质检
          </el-button>
          <!-- 分包 -->
          <el-button
            v-show="scope.row.parentId == 0 && scope.row.taskStatus == 0"
            size="mini"
            type="text"
            icon="el-icon-search"
            @click="handleSplitPackage(scope.row)"
            v-if="checkRoleMethod(['admin'])"
          >
            分包
          </el-button>
          <!-- 抽检 -->
          <el-button
            v-show="scope.row.taskName"
            size="mini"
            type="text"
            icon="el-icon-search"
            @click="handleAddSubTask(scope.row)"
          >
            抽检
          </el-button>
          <!-- 汇总 -->
          <el-button
            v-show="scope.row.parentId != 0 && scope.row.taskStatus == 1 && scope.row.ancestors.split(',').length == 2"
            size="mini"
            type="text"
            icon="el-icon-search"
            @click="handleResultSummarize(scope.row)"
            v-hasPermi="['mark:qatask:deliver']"
          >
            汇总
          </el-button>
          <!-- [1:待标注]的任务验收无误后可交付客户[2:已标注]进行质检操作 -->
          <el-button
            v-show="scope.row.parentId != 0 && (scope.row.taskStatus == 1 || scope.row.taskStatus == 11)"
            size="mini"
            type="text"
            icon="el-icon-info"
            @click="handleDeliver(scope.row)"
            v-hasPermi="['mark:qatask:deliver']"
          >
            交付
          </el-button>
          <!-- [2:已标注]的任务验收后状态改为[3:已验收] -->
          <el-button
            v-show="scope.row.parentId != 0 && (scope.row.taskStatus == 2 || scope.row.taskStatus == 5)"
            size="mini"
            type="text"
            icon="el-icon-info"
            @click="handleValidate(scope.row)"
            v-hasPermi="['mark:qatask:validate']"
          >
            已验
          </el-button>
          <!-- [3:已验收]或[4:返修待确认]状态的任务确认后状态改为[5:已返修] -->
          <el-button
            v-show="scope.row.parentId != 0 && (scope.row.taskStatus == 3 || scope.row.taskStatus == 4)"
            size="mini"
            type="text"
            icon="el-icon-info"
            @click="handleConfirm(scope.row)"
            v-hasPermi="['mark:qatask:confirm']"
          >
            返修确认
          </el-button>
          <!-- [2:已标注]或[5:已返修]状态的任务验收后状态改为[已验收] -->
          <el-button
            v-show="scope.row.parentId != 0 && (scope.row.taskStatus == 2 || scope.row.taskStatus == 5)"
            size="mini"
            type="text"
            icon="el-icon-success"
            @click="handleAccept(scope.row)"
            v-hasPermi="['mark:qatask:accept']"
          >
            通过
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="浏览" width="240" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!-- 配置 -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-show="scope.row.taskName && checkRoleMethod(['admin'])"
            v-hasPermi="['mark:qatask:edit']"
          >
            配置
          </el-button>
          <!-- 列表 -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-notebook-2"
            @click="handleListFile(scope.row)"
            v-show="scope.row.taskName"
          >
            列表
          </el-button>
          <!-- 统计 -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-info"
            @click="handleTaskReport(scope.row)"
            v-show="scope.row.taskName"
          >
            统计
          </el-button>
          <!-- 删除 -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-close"
            @click="handleDelete(scope.row)"
            v-show="scope.row.taskName && checkRoleMethod(['admin'])"
            v-hasPermi="['mark:qatask:remove']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改验收任务对话框 -->
    <el-dialog append-to-body :title="title" :visible.sync="open" width="600px">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="18">
            <el-form-item label="任务名称" prop="taskName">
              <el-input :readonly="title !== '添加验收任务'" v-model="form.taskName" placeholder="请输入验收任务名称" />
            </el-form-item>
          </el-col>
          <el-col :span="24" v-show="checkRoleMethod(['admin'])">
            <el-form-item label="企业" prop="deptId">
              <treeselect v-model="form.deptId" :options="deptOptions" placeholder="选择企业" />
            </el-form-item>
          </el-col>
          <el-col :span="24" v-show="checkRoleMethod(['admin'])">
            <el-form-item label="标注工厂">
              <el-select
                v-model="form.factoryIds"
                multiple
                placeholder="请分配标注工厂"
                :disabled="!checkRoleMethod(['admin'])"
              >
                <el-option
                  v-for="item in factoriesOptions"
                  :key="item.deptId"
                  :label="item.deptName"
                  :value="item.deptId"
                  :disabled="item.status == 1"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="验收任务状态">
              <el-select :disabled="title == '添加验收任务'" v-model="form.taskStatus" placeholder="请选择任务状态">
                <el-option
                  v-for="dict in taskStatusOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="标注类型">
              <el-select v-model="form.markType" placeholder="请选择标注类型">
                <el-option
                  v-for="dict in markTypeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="18">
            <el-form-item label="标注属性字典" prop="markAttributes">
              <el-input v-model="form.markAttributes" placeholder="请输入已配置的标注属性字典名称" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 自动质检参数定义对话框 -->
    <el-dialog title="自动质检参数定义" :visible.sync="checkFormOpen" width="500px" append-to-body>
      <el-form ref="checkForm" :model="checkForm" label-width="120px">
        <el-form-item label="重复的trackId" prop="taskName">
          <el-checkbox v-model="checkForm.duplicateTrackId" placeholder="是否检查重复的trackId" />
        </el-form-item>
        <el-form-item label="空框" prop="percent">
          <el-input v-model="checkForm.emptyCube" placeholder="请输入空框的点数定义，-1表示不查空框" />
        </el-form-item>
        <!--el-form-item label="截断检查" prop="taskName">
          <el-checkbox v-model="checkForm.cutCheck" placeholder="是否检查截断属性" />
        </el-form-item>
        <el-form-item label="遮挡检查" prop="taskName">
          <el-checkbox v-model="checkForm.sheldCheck" placeholder="是否检查遮挡属性" />
        </el-form-item-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitCheckForm">确 定</el-button>
        <el-button @click="cancelCheckForm">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 新建抽检任务对话框 -->
    <el-dialog :title="subTitle" :visible.sync="subOpen" width="500px" append-to-body>
      <el-form ref="subForm" :model="subForm" :rules="rules" label-width="120px">
        <el-form-item label="抽检名称" prop="taskName">
          <el-input v-model="subForm.taskName" placeholder="请输入抽检任务名称" />
        </el-form-item>
        <el-form-item label="记录占比（%）" prop="percent">
          <el-input v-model="subForm.percent" placeholder="请输入抽检记录占总数的百分比" />
        </el-form-item>
        <el-form-item label="开始位置" prop="startNum">
          <el-input v-model="subForm.startNum" placeholder="请输入抽检的开始位置" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitSubForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 分包设置对话框 -->
    <el-dialog title="分包设置" :visible.sync="splitPackageOpen" width="500px" append-to-body>
      <el-form ref="splitPackageForm" :model="splitPackageForm" label-width="120px">
        <el-form-item label="自动分包" prop="isSplit">
          <el-checkbox v-model="splitPackageForm.isSplit" />
        </el-form-item>
        <el-form-item label="需要连trackId" prop="trackId">
          <el-checkbox :disabled="!splitPackageForm.isSplit" v-model="splitPackageForm.trackId" />
        </el-form-item>
        <el-form-item label="每包数量" prop="percent">
          <el-input
            :disabled="!splitPackageForm.isSplit"
            v-model="splitPackageForm.packageCount"
            placeholder="请输入每一子包的数量"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitSplitPackageForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 批量修改任务状态对话框 -->
    <el-dialog append-to-body :title="title" :visible.sync="batchOpen" width="600px">
      <el-form ref="form" :model="batchForm" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="验收任务状态">
              <el-select v-model="batchForm.taskStatus" placeholder="请选择任务状态">
                <el-option
                  v-for="dict in taskStatusOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="企业" prop="deptId">
              <treeselect v-model="batchForm.deptId" :options="deptOptions" placeholder="选择企业" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="标注工厂">
              <el-select v-model="batchForm.factoryIds" multiple placeholder="请分配标注工厂">
                <el-option
                  v-for="item in factoriesOptions"
                  :key="item.deptId"
                  :label="item.deptName"
                  :value="item.deptId"
                  :disabled="item.status == 1"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="标注类型">
              <el-select v-model="batchForm.markType" placeholder="请选择标注类型">
                <el-option
                  v-for="dict in markTypeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="18">
            <el-form-item label="标注属性字典" prop="markAttributes">
              <el-input v-model="batchForm.markAttributes" placeholder="请输入已配置的标注属性字典名称" />
            </el-form-item>
          </el-col>
          <el-col :span="18">
            <el-form-item label="应用到子任务" prop="applySubtask">
              <el-checkbox v-model="batchForm.applySubtask" placeholder="修改的字段应用到子任务" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleBatchOperation">确 定</el-button>
        <el-button @click="batchCancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listQaTask,
    listQaTaskChecker,
    listQaTaskQa,
    getQaTask,
    updateQaTaskStatus,
    delQaTask,
    addQaTask,
    updateQaTask,
    summarize,
    batchSummarize,
    taskAutoCheck,
    taskInitialize,
  } from '@/api/mark/qaTask'
  import { treeselect } from '@/api/system/dept'
  import Treeselect from '@riophae/vue-treeselect'
  import '@riophae/vue-treeselect/dist/vue-treeselect.css'
  import { checkRole } from '@/utils/permission'
  import { downLoadZip } from '@/utils/zipdownload'

  export default {
    name: 'QaTask',
    components: { Treeselect },
    meta: {
      keepAlive: false,
    },
    data() {
      const validateTaskName = (rule, value, callback) => {
        const reg = /^[^\\/:\*\?""<>|$\'￥%# ]{1,120}$/
        let ret = reg.test(this.form.taskName) && reg.test(this.subForm.taskName)
        if (ret) {
          callback()
        } else {
          callback(new Error('任务名称不能包含特殊字符。'))
        }
      }
      return {
        taskId: 0,
        parentId: 0,
        markType: undefined,
        listMethod: undefined,
        deptOptions: [],
        factoriesOptions: [],
        taskPath: '',
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 验收任务表格数据
        qaTaskList: [],
        // 弹出层标题
        title: '',
        // 是否显示弹出层
        open: false,
        batchOpen: false,
        subTitle: '',
        subOpen: false,
        splitPackageOpen: false,
        checkFormOpen: false,
        showMarkAttributesDialog: false,
        // 任务状态字典
        taskStatusOptions: [],
        // 任务类型：0：全检；1：抽检字典
        taskTypeOptions: [],
        // 标注类型字典
        markTypeOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 20,
          parentId: 0,
          deptId: undefined,
          taskName: undefined,
          orderNum: undefined,
          taskStatus: undefined,
          markType: undefined,
        },
        // 表单参数
        form: {},
        batchForm: {
          taskStatus: undefined,
          deptId: undefined,
          factoryIds: undefined,
          markType: undefined,
          markAttributes: undefined,
          applySubtask: false,
        },
        subForm: {},
        splitPackageForm: {
          taskId: null,
          isSplit: true,
          trackId: true,
          packageCount: 100,
        },
        checkForm: {
          taskId: undefined,
          duplicateTrackId: false,
          emptyCube: undefined,
          cutCheck: false,
          sheldCheck: false,
        },
        // 表单校验
        rules: {
          taskName: [
            { required: true, message: '验收任务名称不能为空', trigger: 'blur' },
            { max: 100, message: '最大长度为100个字符', trigger: 'blur' },
            { validator: validateTaskName, trigger: 'blur' },
          ],
        },
      }
    },
    created() {
      this.refresh()
    },
    methods: {
      refresh() {
        if (this.checkRoleMethod(['admin'])) {
          this.listMethod = listQaTask
        } else if (this.checkRoleMethod(['checker', 'marker'])) {
          this.listMethod = listQaTaskChecker
        } else {
          this.listMethod = listQaTaskQa
        }
        this.queryParams.parentId = 0
        if (this.$route.query.taskId || this.$route.query.taskId === 0) {
          this.queryParams.parentId = this.$route.query.taskId
          if (this.$store.getters.queryTask[this.queryParams.parentId]) {
            this.queryParams = this.$store.getters.queryTask[this.queryParams.parentId]
          } else {
            this.queryParams.pageNum = 1
            this.resetForm('queryForm')
          }
          this.taskId = this.$route.query.taskId
        }
        this.getList()
        this.getDicts('mark_qa_task_status').then((response) => {
          this.taskStatusOptions = response.data
        })
        this.getDicts('mark_qa_task_type').then((response) => {
          this.taskTypeOptions = response.data
        })
        this.getDicts('mark_type').then((response) => {
          this.markTypeOptions = response.data
        })
      },
      enterTask(row) {
        this.$router.push({
          path: '/mark/qatask',
          query: {
            taskId: row.taskId,
          },
        })
        // this.queryParams.pageNum = 1
        // this.resetForm('queryForm')
        this.refresh()
      },
      /** 返回上一级按钮操作 */
      handleReturn() {
        getQaTask(this.queryParams.parentId).then((response) => {
          if (response.data) {
            this.$router.replace({
              path: '/mark/qatask',
              query: {
                taskId: response.data.parentId,
              },
            })
            this.refresh()
          }
        })
      },
      enterOnlineCheck(row) {
        if (row.markType == '0')
          this.$router.push({
            path: '/mark/pointCloudSplit',
            query: {
              taskId: row.taskId,
              mQaResultId: row.mQaResultId,
            },
          })
        else
          this.$router.push({
            path: '/mark/editor2d',
            query: {
              taskId: row.taskId,
              mQaResultId: row.mQaResultId,
            },
          })
      },
      checkRoleMethod(role) {
        return checkRole(role)
      },
      /** 查询验收任务列表 */
      getList() {
        this.taskPath = ''
        if (this.$route.query.taskId && this.$route.query.taskId !== 0) {
          getQaTask(this.$route.query.taskId).then((response) => {
            if (response.data) {
              this.taskPath = response.data.taskName
              this.markType = response.data.markType
              if (response.data && response.data.parentId !== 0) {
                getQaTask(response.data.parentId).then((response) => {
                  if (response.data && response.data.parentId !== 0) {
                    this.taskPath = response.data.taskName + '@' + this.taskPath
                    getQaTask(response.data.parentId).then((response) => {
                      if (response.data) {
                        this.taskPath = response.data.taskName + '@' + this.taskPath
                      }
                    })
                  }
                })
              }
            }
          })
        }
        this.loading = true
        this.$store.dispatch('queryTaskCached', this.queryParams)
        this.listMethod(this.queryParams).then((response) => {
          this.qaTaskList = response.rows
          this.total = response.total
          this.loading = false
          getQaTask(this.queryParams.parentId).then((response) => {
            this.$store.state.markData.markAnnotations['QaTask:' + this.queryParams.parentId] = response
            if (response.data) {
              this.getDicts(response.data.markAttributes).then((res1) => {
                let attributeNames = res1.data
                this.$setItem(response.data.markAttributes, attributeNames)
              })
              this.getDicts(response.data.markAttributes + '_colors').then((res1) => {
                let colorNames = res1.data
                this.saveToCacheDB(response.data.markAttributes + '_colors', colorNames)
              })
            }
          })
        })
      },
      saveToCacheDB(_key, _value) {
        this.$setItem(_key, _value, function (err) {
          if (err) {
            console.log('error:')
            console.log(err)
          }
        })
      },
      // 任务状态字典翻译
      taskStatusFormat(row, column) {
        return this.selectDictLabel(this.taskStatusOptions, row.taskStatus)
      },
      // 任务类型：0：全检；1：抽检字典翻译
      taskTypeFormat(row, column) {
        return this.selectDictLabel(this.taskTypeOptions, row.taskType)
      },
      // 标注类型字典翻译
      markTypeFormat(row, column) {
        return this.selectDictLabel(this.markTypeOptions, row.markType)
      },
      // 取消按钮
      cancel() {
        this.open = false
        this.splitPackageOpen = false
        this.subOpen = false
        this.reset()
      },
      batchCancel() {
        if (this.ids.length > 0) {
          this.batchOpen = false
        }
      },
      // 表单重置
      reset() {
        this.form = {
          taskId: undefined,
          parentId: undefined,
          deptId: undefined,
          ancestors: undefined,
          taskName: undefined,
          orderNum: undefined,
          taskStatus: undefined,
          taskType: undefined,
          markType: undefined,
          markAttributes: undefined,
          fileDir: undefined,
          delFlag: undefined,
          createBy: undefined,
          createTime: undefined,
          updateBy: undefined,
          updateTime: undefined,
        }
        this.splitPackageForm = {
          taskId: null,
          isSplit: true,
          trackId: true,
          packageCount: 100,
        }
        this.resetForm('form')
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getList()
      },
      /** 查询企业/机构下拉树结构 */
      getTreeselect() {
        treeselect('0').then((response) => {
          this.deptOptions = response.data
        })
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm('queryForm')
        this.handleQuery()
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map((item) => item.taskId)
        this.single = selection.length != 1
        this.multiple = !selection.length
      },
      checkboxSelect(row) {
        if (row.taskName) {
          return 1
        }
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset()
        this.form.taskStatus = '0'
        this.getTreeselect()
        getQaTask().then((response) => {
          this.factoriesOptions = response.factories
          this.open = true
          this.title = '添加验收任务'
        })
      },
      handleBatch() {
        this.batchOpen = true
        this.getTreeselect()
        getQaTask().then((response) => {
          this.factoriesOptions = response.factories
        })
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset()
        this.getTreeselect()
        getQaTask(row.taskId).then((response) => {
          this.form = response.data
          this.factoriesOptions = response.factories
          this.form.factoryIds = response.factoryIds
          this.open = true
          this.title = '验收任务详情'
        })
      },
      /** 提交按钮 */
      submitForm: function () {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            if (this.form.taskId != undefined) {
              updateQaTask(this.form).then((response) => {
                if (response.code === 200) {
                  this.msgSuccess('修改成功')
                  this.open = false
                  this.getList()
                } else {
                  this.msgError(response.msg)
                }
              })
            } else {
              addQaTask(this.form).then((response) => {
                if (response.code === 200) {
                  this.msgSuccess('新增成功')
                  this.open = false
                  this.getList()
                } else {
                  this.msgError(response.msg)
                }
              })
            }
          }
        })
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const taskIds = row.taskId || this.ids
        this.$confirm('是否确认删除选中的任务?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        })
          .then(() => {
            this.$confirm('再次确认，该任务下所有子任务和标注结果都将被删除，且此删除操作不可恢复！仍要删除？', '警告', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning',
            })
              .then(() => {
                return delQaTask(taskIds)
              })
              .then(() => {
                this.getList()
                this.msgSuccess('删除成功')
              })
              .catch(() => {})
          })
          .catch(() => {})
      },
      handleAutoCheck(row) {
        this.checkForm.taskId = row.taskId
        taskAutoCheck(this.checkForm).then((response) => {
          if (response.code === 200) {
            this.msgSuccess(response.msg)
          }
        })
        // this.checkForm.taskId = row.taskId;
        // this.checkFormOpen = true;
      },
      submitCheckForm() {
        if (!this.checkForm.emptyCube) this.checkForm.emptyCube = -1
        taskAutoCheck(this.checkForm).then((response) => {
          if (response.code === 200) {
            this.msgSuccess(response.msg)
          }
          this.checkFormOpen = false
        })
      },
      cancelCheckForm() {
        this.checkFormOpen = false
        this.checkForm = {
          taskId: undefined,
          duplicateTrackId: false,
          emptyCube: undefined,
          cutCheck: false,
          sheldCheck: false,
        }
      },
      /** 新增抽检任务操作 */
      handleAddSubTask(row) {
        this.subForm.parentId = row.taskId
        this.subOpen = true
        this.subTitle = '抽检结果记录'
      },
      /** 提交抽检任务按钮 */
      submitSubForm: function () {
        this.subForm.taskType = this.subForm.startNum + ':' + this.subForm.percent
        addQaTask(this.subForm).then((response) => {
          if (response.code === 200) {
            this.msgSuccess('抽检任务创建成功')
            this.subOpen = false
            this.getList()
          } else {
            this.msgError(response.msg)
          }
        })
      },
      handleSplitPackage(row) {
        this.splitPackageForm.taskId = row.taskId
        this.splitPackageOpen = true
      },
      /** 提交分包设置按钮 */
      submitSplitPackageForm: function () {
        taskInitialize(this.splitPackageForm).then((res) => {
          this.splitPackageOpen = false
          if (res.code == 200) {
            this.msgSuccess('开始分包，请在大约10分钟后进入任务列表查看。')
          } else {
            this.msgError(res.msg)
          }
        })
      },
      handleResultSummarize(row) {
        this.$confirm('是否汇总该任务下的三级目录的标注结果?', '提示', {
          confirmButtonText: '汇总',
          cancelButtonText: '取消',
          type: 'warning',
        })
          .then(function () {
            return summarize(row.taskId)
          })
          .then(() => {
            this.getList()
            this.msgSuccess('汇总成功，请在5分钟后刷新验收页面检查汇总结果。')
          })
          .catch(function () {})
      },
      /** "交付"按钮操作 */
      handleDeliver(row) {
        this.$confirm('是否向客户交付任务"' + row.taskName + '"的标注结果?', '提示', {
          confirmButtonText: '交付',
          cancelButtonText: '取消',
          type: 'warning',
        })
          .then(function () {
            let para = {
              taskId: row.taskId,
              taskStatus: '2',
            }
            return updateQaTaskStatus(para)
          })
          .then(() => {
            this.getList()
            this.msgSuccess('操作成功')
          })
          .catch(function () {})
        // }).then(() => {
        //   row.taskStatus = "2";
        //   updateQaTask(row).then(response => {
        //       if (response.code === 200) {
        //         this.msgSuccess("修改成功");
        //         this.getList();
        //       } else {
        //         this.msgError(response.msg);
        //       }
        //     });
        // });
      },
      /** "已验"按钮操作 */
      handleValidate(row) {
        this.$confirm('请确实是否已完成任务"' + row.taskName + '"的验收工作并交由标注人员修复发现的标注错误?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        })
          .then(function () {
            let para = {
              taskId: row.taskId,
              taskStatus: '3',
            }
            return updateQaTaskStatus(para)
          })
          .then(() => {
            this.getList()
            this.msgSuccess('操作成功')
          })
          .catch(function () {})
      },
      /** "返修确认"按钮操作 */
      handleConfirm(row) {
        this.$confirm('请确认是否已完成任务"' + row.taskName + '"的返修工作并交由客户重新验收?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        })
          .then(function () {
            let para = {
              taskId: row.taskId,
              taskStatus: '5',
            }
            return updateQaTaskStatus(para)
          })
          .then(() => {
            this.getList()
            this.msgSuccess('操作成功')
          })
          .catch(function () {})
      },
      /** 批量修改状态操作 */
      handleBatchOperation(row) {
        this.$confirm('请确实是否批量修改选中任务状态', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        })
          .then(() => {
            this.batchOpen = false
            let para = {
              taskIds: this.ids,
              taskStatus: this.batchForm.taskStatus,
              deptId: this.batchForm.deptId,
              factoryIds: this.batchForm.factoryIds,
              markType: this.batchForm.markType,
              markAttributes: this.batchForm.markAttributes,
              applySubtask: this.batchForm.applySubtask,
            }
            updateQaTaskStatus(para)
            return
          })
          .then(() => {
            this.getList()
            this.msgSuccess('批量修改成功')
          })
          .catch(function () {})
      },
      handleBatchSummarize(row) {
        let para = {
          taskIds: this.ids,
        }
        batchSummarize(para).then((response) => {
          if (response.code === 200) {
            this.msgSuccess(res.msg)
          } else {
            this.msgError(res.msg)
          }
        })
      },
      /** 通过按钮操作 */
      handleAccept(row) {
        this.$confirm(
          '是否确认标注任务"' +
          row.taskName +
          '"已准确完成并将其标记为通过?任务通过后标注人员将无法再对标注结果进行修改。如需标注人员检查确认，请点击“已验”。',
          '提示',
          {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
          }
        )
          .then(function () {
            let para = {
              taskId: row.taskId,
              taskStatus: '6',
            }
            return updateQaTaskStatus(para)
          })
          .then(() => {
            this.getList()
            this.msgSuccess('操作成功')
          })
          .catch(function () {})
      },
      handleListFile(row) {
        this.$router.push({ path: '/mark/result', query: { taskId: row.taskId, pageNum: 1 } })
      },
      handleTaskReport(row) {
        this.$router.push({ path: '/mark/report', query: { taskId: row.taskId, pageNum: 1 } })
      },
      /** 生成代码操作 */
      handleResultExport(row) {
        const taskId = (row && row.taskId) || this.$route.query.taskId
        if (taskId == '') {
          this.msgError('请选择要导出的任务')
          return
        }
        downLoadZip('/mark/qaTask/exportTaskResult/' + taskId, '')
      },
    },
  }
</script>

<style lang="scss" scoped>
  .filename {
    word-wrap: break-word;
    word-break: break-all;
    display: flex;
    align-items: center;
    height: 46px;

    span {
      line-height: 18px;
    }
  }
</style>
